using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Config;
using CosyTrade.Core;
using CosyTrade.Data;
using CosyTrade.Financial;
using CosyTrade.Organization;
using NUnit.Framework;

namespace CosyTrade.UnitTest
{
    public abstract class AbstractTestCenter
    {
        protected IDistrbutionSystem DistrbutionSystem;
        protected IFinancialSystem FinancialSystem;
        protected IOrganizationSystem OrganizationSystem;
        protected IProductManager ProductManager;
        protected ISecuritySystem SecuritySystem;
        protected IStoreManager StoreManager;
        protected ITradeSystem TradeSystem;
        protected IClientRelationManager CRM;

        protected Company MasterCompany;
        protected Company LiFengCompany;
        protected Company ChangDaCompany;
       
        protected ExchangeRate LiFengExchangeRate ;
        protected ExchangeRate ChangeDaExchangeRate ;
        
        protected Employee CurrentEmployee ;
        
        private AddinServices addinServices;

        [TestFixtureSetUp]
        public void TestSetup()
        {
            addinServices = AddinServices.Instance;
            addinServices.InitalizeAddin();
            InitOrm();
            addinServices.StartAddin();

            AddinManager mgr = addinServices.AddinManager;

            DistrbutionSystem = (IDistrbutionSystem) mgr.GetAddin("DistrbutionSystem");
            FinancialSystem = (IFinancialSystem) mgr.GetAddin("FinancialSystem");
            OrganizationSystem = (IOrganizationSystem) mgr.GetAddin("OrganizationSystem");
            ProductManager = (IProductManager) mgr.GetAddin("ProductManager");
            SecuritySystem = (ISecuritySystem) mgr.GetAddin("SecuritySystem");
            StoreManager = (IStoreManager) mgr.GetAddin("StoreManager");
            TradeSystem = (ITradeSystem) mgr.GetAddin("TradeSystem");
            CRM = (IClientRelationManager) mgr.GetAddin("ClientRelationManager");

            IList<Company> cmpLst = OrganizationSystem.GetCompanyList();
            foreach (Company company in cmpLst)
            {
                string key = company.PrimaryKey.ToString("N").Replace("c", string.Empty);
                if (key.Equals("001"))
                    MasterCompany = company;
                else if (key.Equals("002"))
                    LiFengCompany = company;
                else if (key.Equals("003"))
                    ChangDaCompany = company;
            }

            Guid session = SecuritySystem.Login("admin", "111111", MasterCompany.PrimaryKey);
            CurrentEmployee = SecuritySystem.GetEmployeeBySession(session);

            LiFengExchangeRate = FinancialSystem.GetCompanyDefaultExchangeRate(LiFengCompany.PrimaryKey);
            ChangeDaExchangeRate = FinancialSystem.GetCompanyDefaultExchangeRate(ChangDaCompany.PrimaryKey);
        }

        [TestFixtureTearDown]
        public void TestTearDown()
        {
            addinServices.Stop();
        }

        private static void InitOrm()
        {
            string defaultDbConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "dbConfig.xml");
            XmlConfigurationSource source = new XmlConfigurationSource(defaultDbConfigPath);
            Type[] ormTypes = GetOrmTypes();
            OrmDataAccess.Initialize(source, ormTypes);
        }

        private static Type[] GetOrmTypes()
        {
            Assembly contractAssembly =
                Assembly.LoadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CosyTrade.Contract.dll"));
            Type[] assemblyTypes = contractAssembly.GetTypes();
            List<Type> ormTypes = new List<Type>();
            foreach (Type assemblyType in assemblyTypes)
            {
                if (!assemblyType.IsDefined(typeof(ActiveRecordAttribute), false)) continue;
                if (!assemblyType.IsDefined(typeof(SerializableAttribute), false))
                    throw new Exception(string.Format("{0} Not Mark SerializableAttribute", assemblyType.FullName));
                ormTypes.Add(Type.GetType(assemblyType.AssemblyQualifiedName));
            }
            return ormTypes.ToArray();
        }
    }
}